package repository

import (
	"django_blog_gin/internal/model"
	"django_blog_gin/internal/svc"
	"fmt"
)

type BlogTags struct {
	svcCtx *svc.ServiceContext
}

func NewBlogTags(svcCtx *svc.ServiceContext) *BlogTags {
	return &BlogTags{svcCtx: svcCtx}
}

func (b *BlogTags) CreateBlogTags(articleId int, tagId int) error {
	sql := fmt.Sprintf("INSERT INTO %s (article_id, tag_id) VALUES (?, ?)", model.BlogArticleTags{}.TableName())
	_, err := b.svcCtx.MysqlDb.Exec(sql, articleId, tagId)
	if err != nil {
		return err
	}
	return nil
}

// 批量创建
func (b *BlogTags) CreateBlogTagsBatch(articleId int, tagIds []int) error {
	sql := fmt.Sprintf("INSERT INTO %s (article_id, tag_id) VALUES (?, ?)", model.BlogArticleTags{}.TableName())
	stmt, err := b.svcCtx.MysqlDb.Prepare(sql)
	if err != nil {
		return err
	}
	defer stmt.Close()
	for _, tagId := range tagIds {
		_, err := stmt.Exec(articleId, tagId)
		if err != nil {
			return err
		}
	}
	return nil
}
